/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"github.com/valyala/fasthttp"

	. "saas_sys_base/modules/adminSaas/models"
	. "saas_sys_base/modules/adminSaas/service"
)

type AdminSaasUserCtrl struct {
	service    AdminSaasUserService
	roleSrvice AdminSaasRoleService
}

func init() {
	vc := new(AdminSaasUserCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *AdminSaasUserCtrl) CtrlInfo() string {
	return "AdminSaasUserCtrl"
}

func (vc *AdminSaasUserCtrl) RoutInit() {
	// 列表页面
	lego.GET("/adminSaas/AdminSaasUser/listpage", vc.ListPage, lego.LEGO_SESSION_TYPE_ADMIN, false)
	lego.GET("/adminSaas/AdminSaasUser/listjson", vc.ListJson, lego.LEGO_SESSION_TYPE_ADMIN, false)
	// 编辑页面
	lego.GET("/adminSaas/AdminSaasUser/editpage", vc.GetEditPage, lego.LEGO_SESSION_TYPE_ADMIN, false)
	lego.POST("/adminSaas/AdminSaasUser/editpage", vc.PostEditPage, lego.LEGO_SESSION_TYPE_ADMIN, false)
	// 删除
	lego.POST("/adminSaas/AdminSaasUser/del", vc.DelRecode, lego.LEGO_SESSION_TYPE_ADMIN, false)
}

/* 列表接口
* params:
  ---
*/
func (vc *AdminSaasUserCtrl) ListJson(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := ""
	for {
		// 表单校验
		type ReqParams struct {
			Keyword string // 关键字查询
			Page    int    `vd:"$>=0"` // 分页
			PSize   int    `vd:"$>0"`  // 分页大小
		}
		req := ReqParams{
			Keyword: string(ctxExt.Peek("keyword")),
			Page:    utils.BytesToInt(ctxExt.Peek("page")),
			PSize:   utils.BytesToInt(ctxExt.Peek("limit")),
		}
		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		// 业务查询
		if list, total, err := vc.service.ListAdminSaasUser(req.Keyword, req.Page, req.PSize); err != nil {
			lego.LogError(err)
		} else {
			response["count"] = total
			response["data"] = list
		}
		break
	}

	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *AdminSaasUserCtrl) ListPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	data := make(map[string]interface{})
	for {
		break
	}
	ctxExt.HTML("system/view/AdminSaasUser/list.html", data)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *AdminSaasUserCtrl) GetEditPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	data := make(map[string]interface{})
	for {
		type ReqParams struct {
			Id int64 // 记录id，为空则新建，否则为编辑
		}
		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
		}
		if err := lego.Validate(req); err != nil {
			break
		}

		// userinfo
		target, profile := vc.service.GetAdminSaasUserById(req.Id)
		target.Password = ""
		data["detail"] = target
		data["profile"] = profile
		// role
		roleList := vc.roleSrvice.ListAdminSaasRoleAll()
		data["AdminSaasRoles"] = roleList
		break
	}
	ctxExt.HTML("system/view/AdminSaasUser/edit.html", data)
	return
}

/* 数据编辑
* params:
  ---
*/
func (vc *AdminSaasUserCtrl) PostEditPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	data := make(map[string]interface{})

	code := 0
	msg := ""

	for {
		// 表单校验
		type ReqParams struct {
			Id       int64  `vd:""`
			Account  string `vd:"len($)>0"`
			Password string `vd:""`
			Status   int    `vd:"$>0"`
			UserName string `vd:""`
			Mobile   string `vd:""`
			JobTitle string `vd:""`
			Email    string `vd:""`
			RoleId   int64  `vd:""`
		}
		req := ReqParams{
			Id:       utils.BytesToInt64(ctxExt.Peek("id")),
			Account:  string(ctxExt.Peek("account")),
			Password: string(ctxExt.Peek("password")),
			Status:   utils.BytesToInt(ctxExt.Peek("status")),
			UserName: string(ctxExt.Peek("user_name")),
			Mobile:   string(ctxExt.Peek("mobile")),
			JobTitle: string(ctxExt.Peek("job_title")),
			Email:    string(ctxExt.Peek("email")),
			RoleId:   utils.BytesToInt64(ctxExt.Peek("role_id")),
		}
		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		// 业务处理
		recode, profile := vc.service.GetAdminSaasUserById(req.Id)
		if recode == nil {
			// 新建
			recode := new(AdminSaasUser)

			pwd := vc.service.EncryptPassword(req.Password)
			recode.Password = pwd
			// acc info
			recode.Id = req.Id
			recode.Account = req.Account
			recode.Password = req.Password
			recode.Status = req.Status
			// profile
			profile := new(AdminSaasUserProfile)
			profile.UserName = req.UserName
			profile.Mobile = req.Mobile
			profile.JobTitle = req.JobTitle
			profile.Email = req.Email
			profile.RoleId = req.RoleId

			if false == vc.service.SaveAdminSaasUser(recode, profile) {
				code = 100
				msg = "false"
				break
			}
		} else {
			// acc info
			// 对密码进行加密
			if len(req.Password) > 0 {
				pwd := vc.service.EncryptPassword(req.Password)
				recode.Password = pwd
			}

			recode.Id = req.Id
			recode.Account = req.Account
			recode.Status = req.Status
			// profile
			pfexit := true
			if profile == nil {
				profile = new(AdminSaasUserProfile)
				pfexit = false
			}
			profile.UserName = req.UserName
			profile.Mobile = req.Mobile
			profile.JobTitle = req.JobTitle
			profile.Email = req.Email
			profile.RoleId = req.RoleId

			if false == vc.service.UpdateAdminSaasUser(recode, profile, pfexit) {
				code = 100
				msg = "false"
				break
			}
		}
		break
	}
	response["code"] = code
	response["msg"] = msg
	response["data"] = data
	ctxExt.JSON(200, response)
	return
}

/* 删除数据记录
* params:
  ---
*/
func (vc *AdminSaasUserCtrl) DelRecode(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	data := make(map[string]interface{})

	code := 0
	msg := ""
	for {
		// 表单校验
		type ReqParams struct {
			Id int64 `vd:"$>0"` // 记录id
		}
		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
		}
		if err := lego.Validate(req); err != nil {
			break
		}
		// 业务删除
		target, _ := vc.service.GetAdminSaasUserById(req.Id)
		if target == nil {
			code = 100
			msg = "记录不存在"
			break
		}

		if false == vc.service.DelAdminSaasUserById(target.Id) {
			code = 100
			msg = "操作失败"
			break
		}
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = data
	ctxExt.JSON(200, response)
	return
}
